package com.lizard.schedule.ui.view.schedulelist;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.lizard.schedule.R;

/**
 * 计划列表的item 可手势左右滑动移出选项栏的Layout
 */
public class ScheduleItemLinearLayout extends LinearLayout{

    private final String TAG = ScheduleItemLinearLayout.class.getSimpleName();

    private final int AUTO_SCROLLER_TIME = 500;

    private float mTouchX;

    /**
     * 屏幕外选项view
     */
    private View mOutScreen;

    /**
     * 屏幕外选项view的控件宽度
     */
    private int mOutScreenWidth;

    /**
     * 是否获取过内部尺寸
     */
    private boolean mOutScreenInited = false;

    private Scroller mScroller;

    private VelocityTracker mVelocityTracker;

    /**
     * 测量滑动速度时采用哪个手指点
     */
    private int mPointerId;

    private Context mContext;

    public ScheduleItemLinearLayout(Context context) {
        this(context, null);
    }

    public ScheduleItemLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        mContext = context;
        mScroller = new Scroller(context);
        mVelocityTracker = VelocityTracker.obtain();
        addOnGlobalLayoutListener();
    }

    /**
     * 初始化屏幕外选项
     */
    private void initOutScreen(){
        if(!mOutScreenInited){
            mOutScreenInited = true;
            mOutScreen = findViewById(R.id.out_screen);
            mOutScreenWidth = mOutScreen.getWidth();
        }
    }

    private void addOnGlobalLayoutListener(){
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 获取到尺寸
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                initOutScreen();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 解决ListView滑动时新的item的view addOnGlobalLayoutListener无回调返回的问题
        initOutScreen();

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:

                // 动画还没结束的时候按下 则停在原地
                if(!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }

                mTouchX = event.getX();
                mPointerId = event.getPointerId(0);
                break;
            case MotionEvent.ACTION_MOVE:

                mVelocityTracker.addMovement(event);
                mVelocityTracker.computeCurrentVelocity(1000, ViewConfiguration.get(mContext).getMaximumFlingVelocity());

                int dx = (int)(mTouchX - event.getX());
                if(dx != 0){
                    int scrollX = getScrollX();

                    if(dx > 0){ // 左移
                        if(scrollX == mOutScreenWidth){
                            break;
                        }
                        if(scrollX + dx > mOutScreenWidth){
                            dx = mOutScreenWidth - scrollX;
                        }
                    } else{ // 右移
                        if(scrollX == 0){
                            break;
                        }
                        if(scrollX + dx < 0){
                            dx = 0 - scrollX;
                        }
                    }
                    scrollBy(dx, 0);
                    mTouchX = event.getX();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:

                // 自动根据当前手势速度位置等打开或者关闭选项
                autoToggle();

                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 自动根据当前手势速度位置等展开或者缩起选项
     */
    private void autoToggle(){

        if(getScrollX() != 0 && getScrollX() != mOutScreenWidth){

            //速度
            float v = mVelocityTracker.getXVelocity(mPointerId);
            if(v < -300){
                // 展开
                openOptions();
            } else if(v > 300){
                // 缩起
                closeOptions();
            } else{
                if(getScrollX() > mOutScreenWidth / 2) {
                    // 展开
                    openOptions();
                } else {
                    // 缩起
                    closeOptions();
                }
            }

        }
    }

    /**
     * 展开选项
     */
    public void openOptions(){
        mScroller.startScroll(getScrollX(), 0, mOutScreenWidth - getScrollX(), 0, AUTO_SCROLLER_TIME);
        postInvalidate();
    }

    /**
     * 缩起选项
     */
    public void closeOptions(){
        mScroller.startScroll(getScrollX(), 0, 0 - getScrollX(), 0, AUTO_SCROLLER_TIME);
        postInvalidate();
    }

    /**
     * 判断是否正展开
     * @return
     */
    public boolean isOptionsOpen(){
        return getScrollX() != 0;
    }

    @Override
    public void computeScroll() {
        if(mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }

        super.computeScroll();
    }
}
